﻿Imports ExceptionHandling
Imports System.Threading

Public Class frmMeaUpdate
    Private CheckConn As New CheckInternetConnection()
    'Random Declerations to inizi the Class
    Dim RandomClass As New Random()
    Dim t As Thread

    Private Sub frmUpdate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, MyBase.Activated
        tbVersion.Text = (My.Application.Info.Version.Major & "." & My.Application.Info.Version.Minor & "." & My.Application.Info.Version.Build & "." & My.Application.Info.Version.Revision)
        If CStr(CheckConn.IsConnected()) = False Then
            tbServerVersion.Text = "Unable to contact server"
        Else
            tbServerVersion.Text = "Checking server..."

            t = New Thread(AddressOf RunCheck)
            t.Start()
        End If
    End Sub

    Public Function GetVersion() As String
        Try
            Dim queryUrl As String = [String].Format("http://update.meamod.com/meamodplayme/update/version/?id=" & RandomClass.Next(1000000, 999999999))
            Dim client As New Net.WebClient()
            Dim result As String = client.DownloadString(queryUrl)
            If result = [String].Empty Then
                result = "Unable to contact server"
                Return result
            End If
            Return result
        Catch WebException As Exception
            Return "Unable to contact server"
        End Try
        Return "Unable to contact server"
    End Function

    Public Sub RunCheck()
        Dim ServerText As String
        Dim VersionText As String
        Dim DownloadText As String
        Dim VersionVis As Boolean
        ServerText = GetVersion()
        If Me.tbServerVersion.InvokeRequired Then
            ' It's on a different thread, so use Invoke.
            Dim d As New SetTextCallback(AddressOf SetServer)
            Me.Invoke(d, New Object() {[ServerText]})
        Else
            ' It's on the same thread, no need for Invoke.
            Me.tbServerVersion.Text = ServerText
        End If
        If tbVersion.Text = tbServerVersion.Text Then
            VersionText = "You have the latest version"
            If Me.txtVersion.InvokeRequired Then
                ' It's on a different thread, so use Invoke.
                Dim d As New SetTextCallback(AddressOf SetVersion)
                Me.Invoke(d, New Object() {[VersionText]})
            Else
                ' It's on the same thread, no need for Invoke.
                Me.txtVersion.Text = VersionText
            End If
            wbWhatsNew.Navigate("http://update.meamod.com/meamodplayme/update/whatsnew/" + tbServerVersion.Text + "/?id=" & RandomClass.Next(1000000, 999999999))
        ElseIf tbVersion.Text < tbServerVersion.Text Then
            VersionVis = False
            If Me.txtVersion.InvokeRequired Then
                ' It's on a different thread, so use Invoke.
                Dim d As New SetVisCallback(AddressOf SetVersionVisable)
                Me.Invoke(d, New Object() {[VersionVis]})
            Else
                ' It's on the same thread, no need for Invoke.
                Me.txtVersion.Visible = VersionVis
            End If
            DownloadText = "Update to " & ServerText
            If Me.txtDownload.InvokeRequired Then
                ' It's on a different thread, so use Invoke.
                Dim d As New SetTextCallback(AddressOf SetDownload)
                Me.Invoke(d, New Object() {[DownloadText]})
            Else
                ' It's on the same thread, no need for Invoke.
                Me.txtDownload.Text = DownloadText
            End If
            wbWhatsNew.Navigate("http://update.meamod.com/meamodplayme/update/whatsnew/" + tbServerVersion.Text + "/?id=" & RandomClass.Next(1000000, 999999999))
        ElseIf tbVersion.Text > tbServerVersion.Text Then
            VersionText = "You have a BETA version"
            If Me.txtVersion.InvokeRequired Then
                ' It's on a different thread, so use Invoke.
                Dim d As New SetTextCallback(AddressOf SetVersion)
                Me.Invoke(d, New Object() {[VersionText]})
            Else
                ' It's on the same thread, no need for Invoke.
                Me.txtVersion.Text = VersionText
            End If
            wbWhatsNew.Navigate("http://update.meamod.com/meamodplayme/update/whatsnew/" + tbVersion.Text + "/?id=" & RandomClass.Next(1000000, 999999999))
        End If
    End Sub

    Private Sub SetServer(ByVal [text] As String)
        Me.tbServerVersion.Text = [text]
    End Sub

    Private Sub SetVersion(ByVal [text] As String)
        Me.txtVersion.Text = [text]
    End Sub

    Private Sub SetVersionVisable(ByVal [text] As Boolean)
        Me.txtVersion.Visible = [text]
    End Sub

    Private Sub SetDownload(ByVal [text] As String)
        Me.txtDownload.Text = [text]
    End Sub

    Private Sub lblDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDownload.Click
        Try
            Dim Proc As New System.Diagnostics.Process
            Proc.StartInfo.WorkingDirectory = "updater\"
            Proc.StartInfo.FileName = "MeaUpdater.exe"
            Proc.Start()
            Application.Exit()
        Catch ex As Exception

            Dim CustomError As String
            If VistaControls.OsSupport.IsVistaOrBetter = True Then
                CustomError = "UAC may have disalowed the MeaUpdate Utility from opening"
            Else
                CustomError = "Unable to locate MeaUpdate"
            End If
            Dim CustomUserDo As String
            If VistaControls.OsSupport.IsVistaOrBetter = True Then
                CustomUserDo = "Please accept the UAC promt to launch MeaUpdate." & vbCrLf & "If no UAC promt was shown or you accpeted the UAC promt then please reinstall MeaMod Playme to restore the MeaUpdate Utility"
            Else
                CustomUserDo = "Please reinstall MeaMod Playme to restore the MeaUpdate Utility"
            End If

            If My.Settings.ErrorReporting = True Then
                HandledExceptionManager.EmailError = True
            ElseIf My.Settings.ErrorReporting = False Then
                HandledExceptionManager.EmailError = False
            End If
            HandledExceptionManager.ShowDialog(CustomError, _
                   "You will not be able to update MeaMod Playme", _
                   CustomUserDo, _
                   ex, _
                    MessageBoxButtons.OK, _
                   MessageBoxIcon.Error, _
                   HandledExceptionManager.UserErrorDefaultButton.Default)
        End Try
    End Sub

    Private Sub frmMeaUpdate_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        t.Abort()
    End Sub
End Class